package com.yilin.medical.lsh;

import java.io.File;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.yilin.medical.lsh.Http.HttpApis;

public class MyPostFile {

	public static void main(String[] args) throws Exception {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost post = new HttpPost(HttpApis.uploadFile);

		FileBody fb = new FileBody(new File("C://screen1.bmp"));
		MultipartEntity entity = new MultipartEntity();
		// params.put("type", "pic");
		// params.put("AccessKey", "0c5a3e8915871b710c2cc98073748424");
		// params.put(
		// "SessionID",
		// "52ef5e630++mOgLIsJ779r0k518fFPYAZyGAC9Or0YIg43BYUxkzxNKVfTI7AwucivvwQPg6SuWyUP2Rg0insw");

		entity.addPart("file", fb);
		entity.addPart("type", new StringBody("pic"));
		entity.addPart("AccessKey", new StringBody(
				"0c5a3e8915871b710c2cc98073748424"));
		entity.addPart(
				"SessionID",
				new StringBody(
						"52ef5e630++mOgLIsJ779r0k518fFPYAZyGAC9Or0YIg43BYUxkzxNKVfTI7AwucivvwQPg6SuWyUP2Rg0insw"));
		post.setEntity(entity);
		System.out.println("开始上传");
		HttpResponse response = httpClient.execute(post);
		int code = response.getStatusLine().getStatusCode();
		if (code == HttpStatus.SC_OK) {
			HttpEntity resEntity = response.getEntity();

			System.out.println(EntityUtils.toString(resEntity));// httpclient自带的工具类读取返回数据
			InputStream content = resEntity.getContent();

			System.out.println(content.toString());

			// EntityUtils.consume(resEntity);
		} else {
			System.out.println("失败");
		}
	}
}
